package org.threads.threadpool;

import org.threads.threadpool.task.Task;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Tasks queue template. Commonly is used as
 * <code>TasksQueue<Integer></code>
 *
 * @author oler
 */
public class TasksQueue {

    /**
     * Max capacity of the queue.
     */
    private static final int CAPACITY = 100;

    /**
     * Common <code>Queue</code> structure which
     * keeps tasks.
     */
    private Queue<Task> processQueue;

    /**
     * Real size of queue.
     */
    private int size;

    /**
     * Common constructor.
     */
    public TasksQueue() {
        this.processQueue = new LinkedList<>();
    }

    /**
     * Adds custom task in queue to wait for
     * free ProcessingThread.
     *
     * @param process task
     */
    public void addProcess(Task process) {
        if (process != null && (size < CAPACITY)) {
            processQueue.add(process);
            size++;
        }

    }

    /**
     * Gets task from head and removes it from queue.
     *
     * @return task
     */
    public Task getProcess() {

        Task result = null;

        if (size != 0) {
            result = processQueue.poll();
            size--;
        }

        return result;
    }

    /**
     * Defines if the queue is full in current time.
     *
     * @return
     */
    public boolean isFull() {
        return size == CAPACITY;
    }

    /**
     * Defines if the queue is empty in current time.
     *
     * @return
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * Getter for <code>size</code> field.
     *
     * @return queue size
     */
    public int getSize() {
        return size;
    }

}
